/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *                                                                           *
 *                          Copyright (c) 2010                               *
 *                  Alen Stojanov [ ioi.alen@gmail.com ]                     *
 *                Jacobs University Bremen, Bremen, Germany                  *
 *                                                                           *
 *                          All Rights Reserved.                             *
 *                                                                           *
 *    Permission to use, copy, modify and distribute this software and       *
 *    its documentation is hereby granted, provided that both the            *
 *    copyright notice and this permission notice appear in all copies of    *
 *    the software, derivative works or modified versions, and any           *
 *    portions thereof, and that both notices appear in supporting           *
 *    documentation.                                                         *
 *                                                                           *
 *    This code is distributed in the hope that it will be useful, but       *
 *    WITHOUT ANY WARRANTY; without even the implied warranty of             *
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS       *
 *    DISCLAIM ANY LIABILITY OF ANY KIND FOR ANY DAMAGES WHATSOEVER          *
 *    RESULTING FROM THE USE OF THIS SOFTWARE.                               *
 *                                                                           *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#ifndef MANTRA_H_
#define MANTRA_H_

#include <QGLWidget>
#include <QMutex>

#include "tsdata.h"

/**
 * @class GLLineGraph
 * @author Alen Stojanov, October 2010
 *
 * @brief Represent the data in a Line Graph
 *
 * Render a particular data with given bounds in the
 * preview graph on the TSView object
 */
class GLLineGraph : public QGLWidget
{
	Q_OBJECT

public:
	/**
	 * @brief Default construcructor
	 */
	GLLineGraph(QWidget *parent = 0);
	/**
	 * @brief Remove the particular TS of the preview graph
	 * @param data Pointer to the TS data being removed
	 */
	void removeSeries(TSData *data);

public slots:
	/**
	 * @brief Add a particular TS to the preview graph
	 *
	 * This function will include the data into the graph, and will
	 * repaint the graph such that the new TS is included
	 *
	 * @param data Pointer to the new TS data being added
	 */
	void on_addSeries(QVector<TSData*> data);
	/**
	 * @brief Update the range in the Mantra.
	 * @param rangeMinX	Minumum coordinate of the X-axis
	 * @param rangeMaxX	Maximum coordinate of the X-axis
	 */
	void on_updateRange (double rangeMinX, double rangeMaxX);

	void on_dataHighlight (QVector<TSData*> series);

protected:
	/**
	 * @brief Overloaded method to initialize the OpenGL widget
	 */
	void initializeGL();
	/**
	 * @brief Overloaded method to repaint the OpenGL widget
	 */
	void paintGL();
	/**
	 * @brief Overloaded method to resize the OpenGL widget
	 * @param width 	The width of the viewport
	 * @param height 	The height of the viewort
	 */
	void resizeGL(int width, int height);
	/**
	 * @brief Overload the Mouse press event to detect the last mouse position
	 * @param event 	The default mouse event
	 */
	void mousePressEvent(QMouseEvent *event);
	/**
	 * @brief Overload the Mouse move event
	 * @param event		The default mouse event
	 */
	void mouseMoveEvent(QMouseEvent *event);

private:
	/**
	 * @brief Use a mutex for concurency
	 *
	 * Ensure that when the data is being updated, repaint on the scene has not been
	 * performed (this process might take a long time in certain situations)
	 */
	QMutex mutex;
	/**
	 * @brief Store all the data into one single vector of TSData
	 */
	QVector<TSData*> data;
	/**
	 * @brief True / False to explain whether the data is highlighted or not
	 */
	QVector<bool> highlighedData;
	/**
	 * @brief Count of the higlighted time series
	 */
	int highlighedCount;
	/**
	 * Define the range of the view - minumum of the x-axis
	 */
	double rangeMinX;
	/**
	 * Define the range of the view - maximum of the x-axis
	 */
	double rangeMaxX;
	/**
	 * Ensure that the range has been set, and the graph has been initialized
	 */
	bool isInitialized;
	/**
	 * Define the margin size on the mantra view
	 */
	double marginSize;
	/**
	 * Define the grid size
	 */
	int gridSize;

public:
	/**
	 * @brief The standard destructor
	 */
	~GLLineGraph();
};

#endif /* MANTRA_H_ */
